package com.showmaker.service.impl;

import com.showmaker.constant.MessageConstant;
import com.showmaker.entity.Result;
import com.showmaker.mapper.MemberMapper;
import com.showmaker.mapper.OrderMapper;
import com.showmaker.mapper.SetmealMapper;
import com.showmaker.service.ReportService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@DubboService(interfaceClass = ReportService.class, version = "1.0")
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        try {
            // 当前日期
            String date = format.format(new Date());
            map.put("reportDate", date);

            // 本日新增会员数
            int todayNewMember = memberMapper.getMemberCountByDate(date);
            map.put("todayNewMember", todayNewMember);

            // 总会员数
            int totalMember = memberMapper.getTotalMemberCount();
            map.put("totalMember", totalMember);

            // 获取本周新增会员数
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_WEEK, 2);
            // 获取本周第一天的日期
            Date time = calendar.getTime();
            String firstDayOfWeek = format.format(time);
            calendar.add(Calendar.WEEK_OF_MONTH, 1);
            calendar.add(Calendar.DAY_OF_WEEK, -1);
            // 获取本周最后一天的日期
            time = calendar.getTime();
            String lastDayOfWeek = format.format(time);
            int thisWeekNewMember = memberMapper.getWeekNewMember(firstDayOfWeek, lastDayOfWeek);
            map.put("thisWeekNewMember", thisWeekNewMember);

            // 获取本月新增会员数
            calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            time = calendar.getTime();
            // 本月第一天
            String firstDayOfMonth = format.format(time);
            calendar.add(Calendar.MONTH, 1);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            time = calendar.getTime();
            // 本月最后一天
            String lastDayOfMonth = format.format(time);
            int thisMonthNewMember = memberMapper.getMonthNewMember(firstDayOfMonth, lastDayOfMonth);
            map.put("thisMonthNewMember", thisMonthNewMember);

            // 本日预约数
            int todayOrderNumber = orderMapper.getTodayOrderNumber(date);
            map.put("todayOrderNumber", todayOrderNumber);

            // 本日到诊数
            int todayVisitsNumber = orderMapper.getTodayVisitsNumber(date);
            map.put("todayVisitsNumber", todayVisitsNumber);

            // 本周预约数
            int thisWeekOrderNumber = orderMapper.getThisWeekOrderNumber(firstDayOfWeek, lastDayOfWeek);
            map.put("thisWeekOrderNumber", thisWeekOrderNumber);

            // 本周到诊数
            int thisWeekVisitsNumber = orderMapper.getThisWeekVisitsNumber(firstDayOfWeek, lastDayOfWeek);
            map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);

            // 本月预约数
            int thisMonthOrderNumber = orderMapper.getThisMonthOrderNumber(firstDayOfMonth, lastDayOfMonth);
            map.put("thisMonthOrderNumber", thisMonthOrderNumber);

            // 本月到诊数
            int thisMonthVisitsNumber = orderMapper.getThisMonthVisitsNumber(firstDayOfMonth, lastDayOfMonth);
            map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

            // 获取热门套餐
            List<Map<String, Object>> popularSetmeal = setmealMapper.getPopularSetmeal();
            map.put("hotSetmeal", popularSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
}
